<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="naogen" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 使用el-tree放主题的内容 -->
        <el-tree :data="list" default-expand-all>
          <template #default="{data}">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px;width:100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="naogen" />
                <span>{{data.name}}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{data.manager}}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作
                        <i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children&& data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <el-dialog
        :title="isEdit ? '编辑部门':'新增子部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="resetForm"
      >
        <!-- 弹层中放入子组件 -->
        <addOrEdit :origin-list="originList" @close="showDialog=false" ref="addOrEditRef" :id="curId" :is-edit="isEdit" @success="hSuccess" />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import addOrEdit from "./deptDialog.vue";
import { getDepartments,delDepartment } from "@/api/departments";
import { tranDataToTree } from "@/utils";
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      showDialog: false,//控制新增子部门或者编辑部门的对话框的显示和隐藏
      list: [], //部门列表
      curId: "", //保存当前部门的id
      isEdit: false, //false新增子部门 |  true 编辑部门
      originList:[]//保存所有部门列表的部分(id,pid,name,code)
    };
  },
  created() {
    this.loadDepartments();
  },
  methods: {
    // 重置子组件中的表单
    resetForm(){
      this.$refs.addOrEditRef.resetForm()
    },
    // 删除部门
    hDel(id){
      this.$confirm('是否删除该部门？, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        //  确定
        await delDepartment(id)
        // 重新渲染
        this.loadDepartments()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    hSuccess() {
      //重新渲染
      this.loadDepartments();
      //关闭弹层
      this.showDialog = false;
    },
    // 编辑部门
    hEdit(id) {
      this.curId = id;
      this.isEdit = true;
      this.showDialog = true;

      // this.$nextTick(()=>{
      //   this.$refs.addOrEditRef.loadDepartmentDetail()
      // })
    },
    // 添加
    hAdd(id) {
      // 显示弹层
      this.showDialog = true;
      this.isEdit = false;
      // 更新当前操作的id
      this.curId = id;
    },
    //加载部门列表
    async loadDepartments() {
      // console.log('1231231');
      try {
        const res = await getDepartments();
        console.log("res.data.depts", res.data.depts);

        // 获取部门数据并取出 数组中每个对象的部门数据
        this.originList=res.data.depts.map(item=>{
          return {id:item.id,pid:item.pid,name:item.name, code:item.code}
        })
        //删除数组中第一条数据，取出公司的名称
        res.data.depts.shift();
        this.list = tranDataToTree(res.data.depts);
      } catch (error) {
        console.log(error);
      }
    }
  }
};
</script>